<template>
  <div class="login-container">
    <el-form class="card-box login-form" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
      <img src="../../../static/logo.png" alt="" srcset="" style="margin-left:17%">
      <el-form-item prop="username">
        <span class="svg-container svg-container_login">
          <i class="glyphicon glyphicon-usered"></i>
        </span>
        <el-input name="username"  auto-complete="on" type="text" @keyup.enter.native="handleLogin" v-model="loginForm.username" placeholder="请输入用户名/邮箱" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <i class="glyphicon glyphicon-key"></i>
        </span>
        <el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password"
          placeholder="密码" />
        <span class="show-pwd" @click="showPwd">
          <i class="glyphicon glyphicon-eye"></i>
        </span>
      </el-form-item>
      <!-- <div class="login-forget">
        <el-checkbox v-model="checked">自动登录</el-checkbox>
        <span class="forget-psd">忘记密码</span>
      </div> -->
      <el-button type="primary" style="width:100%;margin-bottom:30px;" :loading="loading" @click.native.prevent="handleLogin">登录</el-button>
    </el-form>
    <div class="login-fix">
      <img src="../../../static/pslogo.png" alt="" srcset="">
      <img src="../../../static/mhwlogo.png" alt="" srcset="">
      <img src="../../../static/cxwz.png" alt="" srcset="">
    </div>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
import {setUser} from '@/utils/timing'
export default {
  name: 'login',
  data () {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      pwdType: 'password',
      loading: false,
      showDialog: false,
      checked: true
    }
  },
  methods: {
    showPwd () {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then((res) => {
            this.loading = false
            if (res.code !== 1) {
              window.localStorage.setItem('token', res.data.token)
              this.$message({
                type: 'error',
                message: res.msg
              })
            } else {
              setUser(res.data)
              this.$message({
                type: 'success',
                message: '登录成功!'
              })
              this.$router.push({ path: '/dashboard' })
            }
          }).catch((err) => {
            console.log(err)
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less">
@import '../../styles/variables.less';

.login-container {
  user-select: none;
  height: 100vh;
  position: relative;
  width: 100%;
  background: url('~/static/bg.png') no-repeat;
  // background-size:100% 950px;
  background-size:100% 100vh;
  input:-webkit-autofill {
    // background-image: url('~/static/bg.png') !important;
    -webkit-box-shadow: 0 0 0px 1000px rgb(60,155,221) inset !important;
    -webkit-text-fill-color: #fff !important;
  }
  input {
    background: transparent;
    border: 0px;
    color: white;
    -webkit-appearance: none;
    border-radius: 0px;
    padding: 12px 5px 12px 15px;
    height: 47px;
  }
  i{
    color: white;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
  }
  .svg-container {
    box-sizing: border-box;
    padding: 6px 5px 6px 15px;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .login-form {
    position: absolute;
    left: 0;
    box-sizing: border-box;
    right: 0;
    width: 400px;
    padding: 150px 35px 15px 35px;
    // background: red;
    margin: 120px auto;
    .login-forget{
      margin-bottom: 30px;
      .forget-psd{
        font-size: 14px;
        color: #CD2626;
        margin-left: 185px;
        cursor: pointer;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgb(60,155,221);
    border-radius: 5px;
    color: #454545;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    cursor: pointer;
    user-select:none;
  }
  .thirdparty-button{
    position: absolute;
    right: 35px;
    bottom: 28px;
  }
  .login-fix{
    position: absolute;
    bottom: 10px;
    right: calc(40px);
    z-index: 999;
    img{
      width: 150px;
      margin-left: 20px;
      float: left;
    }
  }
}
</style>
